package drawmodel;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.media.opengl.GL2;
import javax.swing.JOptionPane;

public class DrawTexture {
	
	// vertex array
	private double[][] vertexSet; 
	//face array
	private int[][] triFaceSet;
	// uv array
	private double[][] uvCorrd;
	
	// set face and vertex array
	boolean arraySet=false;
	
	public DrawTexture(){
		createVertexArray("textured.ttri");
	}
	
	/**
	 * draw the texture model
	 * @param gl The OpenGL context to use for rendering
	 */
	public void draw(GL2 gl,int texture){
		
		   gl.glEnable(GL2.GL_TEXTURE_2D);
	       gl.glActiveTexture(GL2.GL_TEXTURE0);
	       gl.glBindTexture(GL2.GL_TEXTURE_2D, texture);
		   gl.glBegin(GL2.GL_TRIANGLES);
	        for(int i = 0 ; i< triFaceSet.length ; i++)
	        {
	        	gl.glTexCoord2dv(uvCorrd[triFaceSet[i][0]],0);
	        	gl.glVertex3dv(vertexSet[triFaceSet[i][0]], 0);
	        	
	        	gl.glTexCoord2dv(uvCorrd[triFaceSet[i][1]],0);
	        	gl.glVertex3dv(vertexSet[triFaceSet[i][1]], 0);
	        	
	        	gl.glTexCoord2dv(uvCorrd[triFaceSet[i][2]],0);
	        	gl.glVertex3dv(vertexSet[triFaceSet[i][2]], 0);
	        	
	        }
	        
	        gl.glEnd();
	     gl.glDisable(GL2.GL_TEXTURE_2D);
	}
	 /**
	   * Parses the tri file and populates your array of vertex data and faces
    *
	   * @param filename
	   *          The name of the file containing the vertex and faces.
	   */
	  private void createVertexArray(String filename) {
	    BufferedReader reader = null;

	    try {
	      // Attempt to open the file.
	      reader = new BufferedReader(new FileReader(filename));

	      // Read and parse each line.
	      String line = null;
	      int varrayNum=0;
	      int coordNum=0;
	      int farrayNum=0;
	      
	      while ((line = reader.readLine()) != null) {
	    	String[] arr=line.split(" ");
	    	if(arr.length==2){
	    		vertexSet = new double[Integer.parseInt(arr[0])][5];
	    		uvCorrd=new double[Integer.parseInt(arr[0])][2];
	    		
	    		triFaceSet=new int[Integer.parseInt(arr[1])][3];
	    		
	    	}
	    	if(arr.length==5){
	    		vertexSet[varrayNum][0]= Double.parseDouble(arr[0]);
	    		vertexSet[varrayNum][1]= Double.parseDouble(arr[1]);
	    		vertexSet[varrayNum][2]= Double.parseDouble(arr[2]);
	    	    		
	    		uvCorrd[coordNum][0]= Double.parseDouble(arr[3]);
	    		uvCorrd[coordNum][1]= Double.parseDouble(arr[4]);
	    		
	    		varrayNum++;
	    		coordNum++;
	    	}
	    	if(arr.length==4){
	 	    	triFaceSet[farrayNum][0]=Integer.parseInt(arr[1]);
	 	    	triFaceSet[farrayNum][1]=Integer.parseInt(arr[2]);
	 	    	triFaceSet[farrayNum][2]=Integer.parseInt(arr[3]);
	    		farrayNum++;
	    	}
	    	
	    		      
	      }
	      arraySet=true;
	    }
	    catch (FileNotFoundException e) {
	      //e.printStackTrace();
	    	JOptionPane.showMessageDialog(null,"File name not valid \" Re Enter the file name only");
	    	arraySet=false;
	    }
	    catch (IOException e) {
	      e.printStackTrace();
	      arraySet=false;
	    }
	    finally {
	      // Always close the reader, if it has been opened.
	      try {
	        if (reader != null) {
	          reader.close();
	        }
	      }
	      catch (IOException e) {
	        e.printStackTrace();
	      }
	    }
	  }

	
	

}
